package com.example.service.serviceImple;

import com.example.dao.ArticalDao;
import com.example.dao.UserDao;
import com.example.dao.VideoDao;
import com.example.entity.Article;
import com.example.entity.User;
import com.example.entity.Video;
import com.example.service.UserService;
import com.example.util.BeanFactroy;

import java.util.*;

public class UserServiceImpl implements UserService {

    UserDao userDao = BeanFactroy.createBean(UserDao.class);
    VideoDao videoDao = BeanFactroy.createBean(VideoDao.class);
    ArticalDao articalDao = BeanFactroy.createBean(ArticalDao.class);

    //根据id查询用户信息
    @Override
    public User queryUserById(Long id) {
        return userDao.queryUserById(id);
    }

    @Override
    public User queryUserByUsername(String username) {
        User user =  userDao.queryUserByUsername(username);
        if(user!=null){
            return user;
        }
       return null;
    }

    @Override
    public User loginUser(String userName, String password) {
        User user = userDao.loginUser(userName,password);
        return user;
    }

    @Override
    public boolean registerUser(String nickname, String username, String password, String email) {
//        User user = userDao.queryUserByUsername(username);
        //确保手机号邮箱号唯一
        User user = userDao.queryUserByNameEmail(username,email);
        if(user==null){
            boolean bool = userDao.registerUser(nickname,username,password,email);
            return bool;
        }
        return false;
    }

    @Override
    public int updateUserById(User user) {
        return userDao.updateUserById(user);
    }



    @Override
    public List<Object> queryAllWorkByUserId2(long userId) {

        List<Video> videos = videoDao.queryVideoByUserId(userId);
        List<Article> articles = articalDao.findAllArticleByUserId(userId);

        List<Object> allWork = new ArrayList<>();

        boolean fla = false;
       if (videos!=null && articles!=null ){
           int vSize = videos.size();
           int aSize = articles.size();
           for (int i = 0; i < vSize; i++) {
               for (int j = 0; j < aSize; j++) {
                   if (!videos.isEmpty() && !articles.isEmpty()){
                       if (videos.get(0).getCreateTime().getTime()<articles.get(0).getUpdateTime().getTime()){
                           allWork.add(articles.get(0));
                           articles.remove(0);
                       }else {
                           allWork.add(videos.get(0));
                           videos.remove(0);
                           break;
                       }
                   }
                   else {
                       fla = true;
                   }
               }
               if (fla){
                   break;
               }
           }
       }else {
           if (articles !=null && !articles.isEmpty()){
               for (int i = 0; i < articles.size(); i++) {
                   allWork.add(articles.get(i));
               }

           }

           if (videos !=null && !videos.isEmpty()){
               for (int i = 0; i < videos.size(); i++) {
                   allWork.add(videos.get(i));
               }
           }
           return allWork;

       }

        if (!articles.isEmpty()){
            for (int i = 0; i < articles.size(); i++) {
                allWork.add(articles.get(i));
            }

        }

        if (!videos.isEmpty()){
            for (int i = 0; i < videos.size(); i++) {
                allWork.add(videos.get(i));
            }
        }
        return allWork;

    }

    @Override
    public List<Object> searchAllWorkByWord(String word,Long userId) {
        List<Article> articleList = articalDao.SearchByKeyByUserId(word,userId);
        List<Video> videoList = videoDao.SearchByKeyByUserId(word,userId);
        //拼接
        List<Object> objects = new ArrayList<>();
        if(articleList ==null && videoList == null){
            return null;
        }else {
            if (articleList != null){
                for (int i = 0; i < articleList.size(); i++) {
                    objects.add(articleList.get(i));
                }
            }
            if (videoList != null){
                for (int i = 0; i < videoList.size(); i++) {
                    objects.add(videoList.get(i));
                }
            }
            return objects;
        }



    }
}
